<template>
  <div class="permission">
    <el-card>
      <div class="right">
        <el-button @click="isShow(1, '0')">添加权限</el-button>
      </div>
      <el-table :data="permissionData" row-key="id">
        <el-table-column label="菜单名称" prop="name" />
        <el-table-column label="权限标识" prop="code" />
        <el-table-column label="描述" prop="description" />
        <el-table-column label="操作" width="400px">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="isShow(1, row.id)"
            >添加权限页面</el-button>
            <el-button
              v-if="row.type === 1"
              type="text"
              @click="isShow(2, row.id)"
            >添加权限按钮</el-button>
            <el-button type="text" @click="edit(row)">修改权限点</el-button>
            <el-button type="text" @click="del(row)">删除权限点</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <add ref="add" v-model="show" @getData="getData" />
  </div>
</template>

<script>
import { sysPermission, sysPermissionDel } from '@/api/premission'
import add from './components/add.vue'
export default {
  components: { add },
  data() {
    return {
      show: false,
      permissionData: [{}]
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取数据
    async getData() {
      const res = await sysPermission()
      this.permissionData = this.changedata(res.data, '0')
      // console.log(this.permissionData)
    },
    // 转化树形结构
    changedata(arr, pid) {
      return arr.filter((v) => {
        if (v.pid === pid) {
          v.children = this.changedata(arr, v.id)
          return true
        }
      })
    },
    // 新增按钮
    isShow(type, pid) {
      this.show = true
      this.$refs.add.mode = 'add'
      this.$refs.add.form.type = type
      this.$refs.add.form.pid = pid
    },
    // 删除按钮
    del(v) {
      this.$confirm('是否删除?', '温馨提示')
        .then(async() => {
          await sysPermissionDel(v.id)
          this.$message.success('删除成功！')
          this.getData()
        })
        .catch(() => {})
    },
    // 编辑按钮
    edit(v) {
      this.show = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.form = JSON.parse(JSON.stringify(v))
    }
  }
}
</script>

<style lang="scss" scoped>
.permission {
  border: 15px solid #eee;
  .right {
    text-align: right;
  }
}
</style>
